<template>
  <div class="my-content">
    <h3>欢迎来到Maoxx的博客</h3>
    <div class="block">
      <el-avatar :size="50" :src="user.avatar"></el-avatar>
      <div>{{ user.username }}</div>
    </div>

    <div>
      <span>
        <el-link>主页</el-link>
      </span>
      <el-divider direction="vertical"></el-divider>
      <span>
         <el-link type="success" @click="$router.push('/blog/add')">发表博客</el-link>
      </span>

      <span v-show="!hasLogin">
        <el-divider direction="vertical"></el-divider>
         <el-link type="primary" @click="$router.push('/login')">登录</el-link>
      </span>

      <span v-show="hasLogin">
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" @click="logout()">退出</el-link>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      user: {
        username: '请先登录',
        avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
      },
      hasLogin: false
    }
  },
  methods: {
    logout() {
      const _this = this;

      _this.$axios.get('/logout', {
        headers: {
          'Authorization': localStorage.getItem('token')
        }
      }).then(res => {
        _this.$store.commit('REMOVE_INFO')
        _this.$router.push('/login')
      })
    }
  },
  created() {
    if (this.$store.getters.GET_INFO.username) {
      this.user.username = this.$store.getters.GET_INFO.username
      this.user.avatar = this.$store.getters.GET_INFO.avatar
      this.hasLogin = true
    }
  }
}
</script>

<style scoped>
.my-content {
  text-align: center;
  max-width: 960px;
  margin: 0 auto;
}
</style>